package com.ossjk.qlh.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.core.vo.RequestBean;
import com.ossjk.core.vo.ResponseBean;
import com.ossjk.qlh.crm.entity.Enterprise;
import com.ossjk.qlh.crm.entity.Member;
import com.ossjk.qlh.crm.service.IEnterpriseService;
import com.ossjk.qlh.crm.service.IMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 成员
 *
 * @author parker
 * @since 2020-08-12
 */
@Api(tags = "成员")
@RestController
@RequestMapping("/crm/member")
public class MemberController extends BaseController {

    @Autowired
    private IMemberService iMemberService;
    @Autowired
    private IEnterpriseService iEnterpriseService;


    @ApiOperation(value = "列表")
    @RequiresPermissions("003000020000")
    @PostMapping("/list")
    public ResponseBean<Page<Member>> list(@RequestBody RequestBean requestBean) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<Member>();

        String eid = (String) requestBean.getParam().get("id");
        queryWrapper.eq("eid", eid);

        return responseSuccess(iMemberService.page(requestBean.getPage(), queryWrapper));
    }

    //<p>合同签约人数:<span>xxx</span>人，当前注册人数:<span>xxx</span>,其中未确认：<span>xxx</span>人,<span>xxx</span>人已确认</p>
    @ApiOperation(value = "成员分析")
    @RequiresPermissions("003000020000")
    @PostMapping("/listcount")
    public ResponseBean listcount(@RequestBody RequestBean requestBean) {


        String eid = (String) requestBean.getParam().get("id");
        //获取企业名
        Enterprise byId = iEnterpriseService.getById(eid);
        String ename = "";
        if (byId != null)
            ename = byId.getEname();
        //合同签约人数/当前注册人数

        QueryWrapper<Member> queryWrapper = new QueryWrapper<Member>();
        queryWrapper.eq("eid", eid);
        int count = iMemberService.count(queryWrapper);

        //未确认人数
        QueryWrapper<Member> queryWrapper2 = new QueryWrapper<Member>();
        queryWrapper2.eq("eid", eid).eq("state", 1);
        int count1 = iMemberService.count(queryWrapper2);

        //已确认人数
        QueryWrapper<Member> queryWrapper3 = new QueryWrapper<Member>();
        queryWrapper3.eq("eid", eid).eq("state", 2);
        int count2 = iMemberService.count(queryWrapper3);

        Map map = new HashMap();
        map.put("ename", ename);
        map.put("count", count);
        map.put("count1", count1);
        map.put("count2", count2);

        List list = new ArrayList();
        list.add(map);

        return responseSuccess(list);
    }

    @ApiOperation(value = "删除")
    @RequiresPermissions("003000020000")
    @PostMapping("/remove")
    public ResponseBean delete(@RequestBody RequestBean requestBean) {
        List ids = (List) requestBean.getParam().get("ids");
        if (iMemberService.removeByIds(ids)) {
            return responseSuccess();
        } else {
            return responseFail();
        }
    }

    @ApiOperation(value = "更改用户状态")
    @RequiresPermissions("003000020000")
    @ApiOperationSupport(ignoreParameters = {"crer", "mder", "crtm", "mdtm"})
    @PostMapping("/changestate")
    public ResponseBean changeState(@RequestBody RequestBean<Member> requestBean) {
        String id = (String) requestBean.getParam().get("id");
        Integer state = (Integer) requestBean.getParam().get("state");
        System.out.println(state);

        Member member = iMemberService.getById(id);
        if (CommonUtil.isBlank(member)) {
            return responseFail("数据异常");
        }
        member.setState(state);

        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("id",id);

        if (iMemberService.update(member,wrapper)) {
            return responseSuccess("更改成功");
        } else {
            return responseFail("数据异常");
        }
    }


    @ApiOperation(value = "添加工号")
    @RequiresPermissions("003000020000")
    @ApiOperationSupport(ignoreParameters = {"crer", "mder", "crtm", "mdtm"})
    @PostMapping("/addmnum")
    public ResponseBean addMnum(@RequestBody RequestBean<Member> requestBean) {
        String id = (String) requestBean.getParam().get("id");
        String mnum = (String) requestBean.getParam().get("mnum");
        System.out.println(mnum);

        Member member = iMemberService.getById(id);
        if (CommonUtil.isBlank(member)) {
            return responseFail("数据异常");
        }
        member.setMnum(mnum);

        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("id",id);

        if (iMemberService.update(member,wrapper)) {
            return responseSuccess("更改成功");
        } else {
            return responseFail("数据异常");
        }
    }

}
